 /**
   * Copyright 2014 Viettel Telecom. All rights reserved.
   * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   */
 package com.viettel.core.dao.cms; 
import java.util.ArrayList;
import java.util.List;

import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired; 

import com.viettel.core.common.utils.StringUtil;
import com.viettel.core.exceptions.DataAccessException; 
import com.viettel.core.entities.cms.Article;
import com.viettel.core.entities.cms.CmsUser;
import com.viettel.core.entities.cms.GroupCategory;
import com.viettel.core.entities.cms.Media;
import com.viettel.core.entities.cms.RegimensDetail;
import com.viettel.core.entities.cms.TechniqueCategory;
import com.viettel.core.entities.vo.ArticleSearchVO;
import com.viettel.core.entities.vo.QuestionSearchVO;
import com.viettel.core.entities.vo.RegimenSearchFilter;
import com.viettel.core.entities.vo.RegimenVO;
import com.viettel.core.enumtype.RegimensDetailStatus;
import com.viettel.core.repo.IRepository;
 /**
   *  Mo ta muc dich cua lop (interface)
   *  @author: TOANTM1
   *  @version:  
   *  @since: 27/02/2014 - 11:34:02
   */
 public class RegimensDetailDAOImpl implements RegimensDetailDAO { 
	 private IRepository repo; 
		
	 	public void setRepo(IRepository repo) {
			this.repo = repo;
		}
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public RegimensDetail getRegimensDetailById(Integer id) throws DataAccessException { 
		return repo.getEntityById(RegimensDetail.class, id); 
	} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public RegimensDetail createRegimensDetail(RegimensDetail regimensDetail) 
			throws DataAccessException { 
		if (regimensDetail == null) { 
			throw new IllegalArgumentException("RegimensDetail"); 
		} 
		repo.create(regimensDetail); 
		return repo.getEntityById(RegimensDetail.class, regimensDetail.getId()); 
	} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public void updateRegimensDetail(RegimensDetail regimensDetail) throws DataAccessException { 
		if (regimensDetail == null) { 
			throw new IllegalArgumentException("RegimensDetail"); 
		} 
		repo.update(regimensDetail); 
	} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public void deleteRegimensDetail(RegimensDetail regimensDetail) throws DataAccessException { 
		if (regimensDetail == null) { 
			throw new IllegalArgumentException("regimensDetail"); 
		} 
		repo.delete(regimensDetail); 
	}

	@Override
	public List<RegimenVO> getListRegimensDetailByArticleId(Integer id)
			throws DataAccessException {
		List<Object> params = new ArrayList<Object>();
		String sql = "SELECT id 						as id, "
				+ " 	TO_CHAR(from_Date,'dd/MM/yyyy') as fromDate, "
				+ " 	TO_CHAR(to_Date,'dd/MM/yyyy') 	as toDate," 
				+ " 	from_Age 						as fromAge, "
				+ "		to_Age 							as toAge, "
				+ "		content 						as content " 
				+ " FROM regimens_detail "
				+ " WHERE article_id = ? and status != ? "
				+ " ORDER BY id "; 
		
		params.add(id);
		params.add(RegimensDetailStatus.DELETED.getValue());
		
		String[] fieldNames = new String[] { 
				"id", "fromDate", "toDate", "fromAge", "toAge", "content"
				};
		Type[] fieldTypes = new Type[] { 
				StandardBasicTypes.INTEGER, 
				StandardBasicTypes.STRING, StandardBasicTypes.STRING, 
				StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER,
				StandardBasicTypes.STRING };

		List<Class<?>> synchronizedClass = new ArrayList<Class<?>>();
		synchronizedClass.add(RegimensDetail.class);

		List<RegimenVO> list = null;
		
		list = repo.getListByQueryAndScalar(RegimenVO.class,
					fieldNames, fieldTypes, sql, params,
					synchronizedClass);
		return list;
	}

	@Override
	public List<RegimenVO> getListRegimensDetailByFilter(
			RegimenSearchFilter filter) throws DataAccessException {
		List<Object> paramSelect = new ArrayList<Object>();
		StringBuilder selectQuery = new StringBuilder();
		StringBuilder fromQuery = new StringBuilder();
		StringBuilder countQuery = new StringBuilder();
		
		selectQuery.append(" SELECT RD.id 						as id, ");
		selectQuery.append(" TO_CHAR(RD.from_Date,'dd/MM/yyyy') as fromDate, ");
		selectQuery.append(" TO_CHAR(RD.to_Date,'dd/MM/yyyy') 	as toDate, ");
		selectQuery.append(" RD.from_Age 						as fromAge, ");
		selectQuery.append(" RD.to_Age 							as toAge, ");
		selectQuery.append(" RD.content 						as content "); 
		
		fromQuery.append(" FROM regimens_detail RD ");
		fromQuery.append(" JOIN article AT ON AT.id = RD.article_id ");
		fromQuery.append(" WHERE 1=1 AND RD.status != -1 AND AT.status!= -1");
		
		if(filter.getArticleId()!=null){
			fromQuery.append(" AND RD.article_id = ? ");
			paramSelect.add(filter.getArticleId());
		}
		if(filter.getContent()!=null){
			fromQuery.append(" AND upper(RD.content_text) like upper(?) ESCAPE '/' ");
			paramSelect.add("%" + StringUtil.toSearchText(filter.getContent())+ "%");
		}
		if(filter.getFromDate()!=null){
			fromQuery.append(" AND trunc(RD.from_Date) >=  trunc(?) ");
			paramSelect.add(filter.getFromDate());
		}
		if(filter.getToDate()!=null){
			fromQuery.append(" AND trunc(RD.to_Date) <= trunc(?) ");
			paramSelect.add(filter.getToDate());
		}
		if(filter.getFromAge()!=null){
			fromQuery.append(" AND RD.from_Age >= ? ");
			paramSelect.add(filter.getFromAge());
		}
		if(filter.getToAge()!=null){
			fromQuery.append(" AND RD.to_Age <= ? ");
			paramSelect.add(filter.getToAge());
		}
		if(filter.getGroupCategoryId()!=null){
			fromQuery.append(" AND AT.group_category_id = ? ");
			paramSelect.add(filter.getGroupCategoryId());
		}
		selectQuery.append(fromQuery);
		countQuery.append("select count(*) as count ").append(fromQuery);
		
		String[] fieldNames = new String[] { 
				"id", "fromDate", "toDate", "fromAge", "toAge", "content"
				};
		Type[] fieldTypes = new Type[] { 
				StandardBasicTypes.INTEGER, 
				StandardBasicTypes.STRING, StandardBasicTypes.STRING, 
				StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER,
				StandardBasicTypes.STRING };

		List<Class<?>> synchronizedClass = new ArrayList<Class<?>>();
		synchronizedClass.add(RegimensDetail.class);
		synchronizedClass.add(Article.class);

		List<RegimenVO> list = null;
		if (filter.getPaging() != null) {
			list = repo.getListByQueryAndScalarPaginated(
					RegimenVO.class, fieldNames, fieldTypes,
					selectQuery.toString(), countQuery.toString(), paramSelect,
					paramSelect, filter.getPaging(), synchronizedClass);
		} else {
			list = repo.getListByQueryAndScalar(RegimenVO.class,
					fieldNames, fieldTypes, selectQuery.toString(), paramSelect,
					synchronizedClass);
		}
		return list;
	} 
} 
 
